package cn.ejiajunxy.manager.fragment;

import android.os.Bundle;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.leo.baseui.mutiType.base.Items;
import com.leo.baseui.mutiType.listFragment.ListFragment2;
import com.leo.utils.CheckUtils;

import cn.ejiajunxy.manager.adapter.OrderAdapter;
import cn.ejiajunxy.manager.api.ApiService;
import cn.ejiajunxy.manager.common.AppObserver;
import cn.ejiajunxy.manager.model.OrderModel;
import cn.ejiajunxy.manager.model.StatusModel;
import cn.ejiajunxy.manager.provider.OrderListBeanProvider;
import cn.ejiajunxy.manager.utils.ToastUtils;
import cn.ejiajunxy.manager.utils.UserPrefManager;

/**
 * Created by zhangguobao on 2018/3/11.
 */

public class FragmentOrderList extends ListFragment2<OrderAdapter> {
    private static final String KEY_TYPE = "type";
    private int mType;
    private OrderAdapter mOrderAdapter;

    public static FragmentOrderList newInstance(int type) {
        Bundle args = new Bundle();
        FragmentOrderList fragment = new FragmentOrderList();
        args.putInt(KEY_TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public OrderAdapter getAdapter() {
        return mOrderAdapter;
    }

    @Override
    protected void preInit() {
        super.preInit();
        if (getArguments() != null) {
            mType = getArguments().getInt(KEY_TYPE);
        }
        mOrderAdapter = new OrderAdapter(mType == 0 ? true : false, new OrderListBeanProvider.Callback() {
            @Override
            public void onSend(String orderId) {
                showNoDataLoading();
                RxHttpUtils.createApi(ApiService.class)
                        .updateStatus(UserPrefManager.getToken(getActivity()), orderId, "5")
                        .compose(Transformer.<StatusModel>switchSchedulers())
                        .subscribe(new AppObserver<StatusModel>() {
                            @Override
                            protected void success(StatusModel statusModel) {
                                if (statusModel == null || statusModel.status != 0) {
                                    ToastUtils.showToast(getContext(), "更新失败");
                                    return;
                                }
                                ToastUtils.showToast(getContext(), "已配送更新成功");
                                requestData();
                            }

                            @Override
                            protected void onError(String s) {
                                hideNoDataLoading();
                                ToastUtils.showNetError(getContext());
                            }
                        });
            }
        });
    }

    @Override
    public void onInit(Bundle var1) {
        setHasLoadMore(false);
        requestData();
    }

    private void requestData() {
        RxHttpUtils.createApi(ApiService.class)
                .getOrder(UserPrefManager.getToken(getActivity()), mType == 0 ? "2" : "5")
                .compose(Transformer.<OrderModel>switchSchedulers())
                .subscribe(new AppObserver<OrderModel>() {
                    @Override
                    protected void onError(String s) {
                        handlerErrorMsg(true, null, null);
                    }

                    @Override
                    protected void success(OrderModel myOrderModel) {
                        hideNoDataLoading();
                        setRefreshComplete();
                        if (myOrderModel == null || myOrderModel.status != 0) {
                            handlerErrorMsg(true, "获取订单失败", "获取订单失败");
                        }
                        if (CheckUtils.isEmpty(myOrderModel.orderList)) {
                            mOrderAdapter.getItems().clear();
                            mOrderAdapter.notifyDataSetChanged();
                            showNoDataErrorMsg("暂无订单内容");
                        } else {
                            Items items = new Items();
                            items.addAll(myOrderModel.orderList);
                            setItems(items);
                        }
                    }
                });
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onRefresh() {
        requestData();
    }
}
